package net.lainiao.mycost.service.impl;

import net.lainiao.mycost.bean.AuditHistory;
import net.lainiao.mycost.bean.ExpenseAccount;
import net.lainiao.mycost.bean.ExpenseDetails;
import net.lainiao.mycost.dao.IExpenseDAO;
import net.lainiao.mycost.service.IExpenseService;
import net.lainiao.mycost.utils.Comm;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DeadlockLoserDataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/22.
 */
@Service
@Transactional
public class ExpenseServiceImpl implements IExpenseService {

    @Autowired
    private IExpenseDAO expenseDAO;

    @Override
    public void add(ExpenseAccount account, Integer[] costids, Double[] ExpenseDetailsSmount) {
        expenseDAO.addExpense(account);
        for (int i = 0; i < costids.length; i++) {
            ExpenseDetails details=new ExpenseDetails();
            details.setExpenseId(account.getExpenseId());
            details.setCostId(costids[i]);
            details.setExpenseDetailsAmount(ExpenseDetailsSmount[i]);
            expenseDAO.addExpenseDeatils(details);
        }
    }

    @Override
    public void update(ExpenseAccount account, Integer[] costIds, Double[] ExpenseDetailsSmount) {
        expenseDAO.updateExpenseAccount(account);
        expenseDAO.deleteExpenseDetails(account.getExpenseId());
        for (int i = 0; i < costIds.length; i++) {
            ExpenseDetails details=new ExpenseDetails();
            details.setExpenseId(account.getExpenseId());
            details.setCostId(costIds[i]);
            details.setExpenseDetailsAmount(ExpenseDetailsSmount[i]);
            expenseDAO.addExpenseDeatils(details);
        }
    }

    @Override
    public List<ExpenseAccount> list(ExpenseAccount account) {
        condition(account);
        return expenseDAO.list(account);
    }

    private void condition(ExpenseAccount account) {
        if(account!=null){
            if(StringUtils.isNotEmpty(account.getUserName())){
                account.setUserName("%"+account.getUserName()+"%");
            }
        }
    }

    @Override
    public long getCount(ExpenseAccount account) {
        condition(account);
        return expenseDAO.getCount(account);
    }

    @Override
    public ExpenseAccount getExpenseAccount(ExpenseAccount account) {

        return expenseDAO.getExpenseAccount(account);
    }

    @Override
    public List<Map> getExpenseDetailsList(ExpenseAccount account) {
        return expenseDAO.getExpenseDetailsList(account);
    }

    @Override
    public void auditManager(AuditHistory history) {
        ExpenseAccount account=new ExpenseAccount();
        account.setExpenseId(history.getExpenseId());
        if(Comm.EXPPENSE_STATE_REONE.equals(history.getAuditState())){
            account.setExpenseState(Comm.EXPPENSE_STATE_ZERO);
        }else if(Comm.EXPPENSE_STATE_DELETE.equals(history.getAuditState())){
            account.setExpenseState(Comm.EXPPENSE_STATE_DELETE);
        }
        else{
            account.setExpenseState(history.getAuditState());
        }

        expenseDAO.updateExpenseAccount(account);
        expenseDAO.addAuditHistory(history);
    }

    @Override
    public void auditFinance(AuditHistory history) {
        ExpenseAccount account=new ExpenseAccount();
        account.setExpenseId(history.getExpenseId());
        String histState=history.getAuditState();
        if(Comm.EXPPENSE_STATE_RETWO.equals(histState)){
            account.setExpenseState(Comm.EXPPENSE_STATE_ZERO);
        }else if(Comm.EXPPENSE_STATE_DELETE.equals(histState)){
            account.setExpenseState(Comm.EXPPENSE_STATE_DELETE);
        }else{
            account.setExpenseState(histState);
        }
        expenseDAO.updateExpenseAccount(account);
        expenseDAO.addAuditHistory(history);
    }

    @Override
    public List<Map> getAuditHistoryList(ExpenseAccount account) {
        return expenseDAO.getAuditHistoryList(account);
    }

    @Override
    public List<ExpenseAccount> getList(ExpenseAccount account) {
        condition(account);
        return expenseDAO.getList(account);
    }

    @Override
    public long getCountLong(ExpenseAccount account) {
        condition(account);
        return expenseDAO.getCountLong(account);
    }
}
